﻿using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormSelectSalesperson : FormTemplet
    {
        private static string s_ResultString = null;

        private FormSelectSalesperson()
        {
            this.InitializeComponent();
        }

        private FormSelectSalesperson(string sCaption) : this()
        {
            this.Text = sCaption;
            FormSelectSalesperson.s_ResultString = null;
            Orders orders = new Orders();
            DataTable dataTable = null;
            try
            {
                dataTable = orders.GetSalespersons();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n出现未知错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            orders.Dispose();
            if (dataTable != null)
            {
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    this.cbSalesperson.Items.Add(dataTable.Rows[i][0].ToString());
                }
                dataTable.Dispose();
            }
            this.cbSalesperson.SelectedIndex = 0;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            FormSelectSalesperson.s_ResultString = this.cbSalesperson.Text;
            base.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            FormSelectSalesperson.s_ResultString = null;
            base.Close();
        }

        public static string ShowBox(string sCaption)
        {
            FormSelectSalesperson formSelectSalesperson = new FormSelectSalesperson(sCaption);
            formSelectSalesperson.ShowDialog();
            return FormSelectSalesperson.s_ResultString;
        }
    }
}
